{% extends "base-left.html" %}
{% load staticfiles %}

{% block css %}
<link rel="stylesheet" href="{% static 'plugins/datatables/jquery.dataTables.min.css' %}">
<link rel="stylesheet" href="{% static 'js/plugins/layer/skin/layer.css' %}">
<link rel="stylesheet" href="{%static 'plugins/select2/select2.min.css' %}">
<!-- iCheck for checkboxes and radio inputs -->
{% endblock %}

{% block content %}

<!-- Content Header (Page header) -->

<section class="content-header margin-bottom">
  <ol class="breadcrumb">
    <li><a href=""><i class="fa fa-dashboard"></i>办公平台</a></li>
    {% if menu.parent.parent %}
    <li class="active"><a href="{{ menu.parent.parent.url }}">{{ menu.parent.parent.title }}</a></li>
    {% endif %}
    <li class="active"><a href="{{ menu.url }}">{{ menu.title }}</a></li>
  </ol>
</section>
    <!-- Main content -->
  <section class="content">
     <div id="devlist">
        <div class="box box-primary" id="liebiao">
            <div class="box-header">
                <div class="btn-group pull-left">
                    <button type="button" id="btnCreate" class="btn btn-primary">
                        <i class="glyphicon glyphicon-plus"></i>入库
                    </button>

                </div>
                <div class="btn-group pull-left">&nbsp</div>
                <div class="btn-group pull-left">
                    <button type="button" id="btnDelete" class="btn btn-danger">
                        <i class="glyphicon glyphicon-trash"></i>删除
                    </button>
                </div>
            </div>
            <div class="box-header">
              <form class="form-inline" id="queryForm">
                <div class="form-group searchArea margin-r-5 margin-top-5">
                    <label>设备编号：</label>
                    <input type="text" name="assetNum" class="form-control inputText" id="assetNum">
                </div>
               <div class="form-group searchArea margin-r-5 margin-top-5">
                <label>资产类型：</label>
                <select class="form-control inputText select2" name="assetType", id="assetType">
                    <option></option>
                    {% for type in asset_types %}
                    <option value={{ type.id }}>{{ type.name }}</option>
                    {% endfor %}
                </select>
              </div>
                <div class="form-group searchArea margin-r-5 margin-top-5">
                    <label>资产型号：</label>
                    <input type="text" name="model" class="form-control inputText" id="model">
                </div>
                 <div class="form-group searchArea margin-r-5 margin-top-5">
                    <label>资产状态：</label>
                     <select class="form-control inputText select2" name="status", id="status">
                        <option></option>
                        {% for status in status_list %}
                        <option value={{ status.item }}>{{ status.value }}</option>
                        {% endfor %}
                    </select>
               </div>
                <button type="button" id="btnAssetSearch" class="btn btn-default">
                    <i class="glyphicon glyphicon-search"></i>查询
                </button>
            </form>
            </div>
             <div class="box-body">
                <table id="dtbList" class="display" cellspacing="0" width="100%">
                    <thead>
                    <tr valign="middle">
                        <th><input type="checkbox" id="checkAll"></th>
                        <th>ID</th>
                        <th>设备编号</th>
                        <th>类型</th>
                        <th>品牌</th>
                        <th>型号</th>
                        <th>仓库</th>
                        <th>状态</th>
                        <th>使用人</th>
                        <th>入库人</th>
                        <th>入库时间</th>
                        <th>操作</th>
                    </tr>
                    </thead>
                    <tbody>
                    </tbody>
                </table>
                <br> <br>
            </div>
        </div>
     </div>
  </section>

    <!-- /.content -->

{% endblock %}

{% block javascripts %}
  <script src="{% static 'plugins/datatables/jquery.dataTables.min.js' %}"></script>
  <script src="{% static 'plugins/datatables/dataTables.const.js' %}"></script>
  <script src="{% static 'plugins/datatables/moment.min.js' %}"></script>
  <script src="{% static 'js/plugins/layer/layer.js' %}"></script>
  <script src="{% static 'plugins/select2/select2.full.min.js' %}"></script>
  <!-- iCheck 1.0.1 -->
  <script type="text/javascript">
	$(function() {
	    $('#ADM-ASSET').addClass('active');

	});
  </script>

    <script type="text/javascript">
var oDataTable=null;
$(function() {
	oDataTable = initTable();
	function initTable() {
		var oTable=$('#dtbList').DataTable($.extend(true,{},
						DATATABLES_CONSTANT.DATA_TABLES.DEFAULT_OPTION,
						{
							ajax : {
								"url":"{% url 'adm-asset:list' %}",
								"data":function ( d ) {
									d.assetNum=$("#assetNum").val();
									d.assetType=$("#assetType").val();
									d.model=$("#model").val();
									d.status=$("#status").val();
								}
						},

						columns : [
								DATATABLES_CONSTANT.DATA_TABLES.COLUMN.CHECKBOX,
								{
									data : "id",
								},
								{
									data : "assetNum",
								},


                                {
									data : "assetType__name",
								},
                                {
									data : "brand",
								},
                                {
									data : "model",
								},
                                {
                                    data: "warehouse",
                                     render : function(data, type, row, meta) {
										if (data==0){
                                            var ret="南京";
											return ret;
										}if(data==1) {
                                             var ret = "苏州";
                                             return ret;
                                         }
										}
                                },
                                {   data : "status",
                                   render : function(data, type, row, meta) {
										if (data==0){
                                            var ret="<button class='btn btn-primary btn-xs'>闲置</button>";
											return ret;
										}if(data==1){
											var ret="<button class='btn  btn-success btn-xs'>在用</button>";
											return ret;

										}if(data==2) {
                                            var ret="<button class='btn btn-warning btn-xs'>维修</button>";
											return ret;

                                        }if(data==3) {
                                            var ret="<button class='btn btn-danger btn-xs'>报废</button>";
											return ret;
                                        }if(data==4) {
                                            var ret="<button class='btn btn-info btn-xs'>售出</button>";
											return ret;
                                        }
									}
								},
                                {
									data : "owner__name",
								},
                                {
									data : "operator",
								},
                                {
									data : "add_time",
                                     render : function(data, type, full, meta) {
                                         return  moment(data).format("YYYY-MM-DD HH:mm");}
								},

								{
									data : "id",
									{#width : "10%",#}
									bSortable : "false",
									render : function(data, type, row, meta) {
										var ret="<button title='详情' onclick='doDetail("
										+ data + ")'><i class='glyphicon glyphicon-list-alt'></i></button>";
                                        ret=ret+"<button title='编辑' onclick='doUpdate("
										+ data + ")'><i class='glyphicon glyphicon-pencil'></i></button>";
										 ret=ret+"<button title='删除' onclick='doDelete("
                                            + data + ")'><i class='glyphicon glyphicon-trash'></i></button>";
										return ret;
									}
								} ],
						}));
		return oTable;
	}

   //刷新页面GET查询请求
    $("#btnAssetSearch").click(function(){
            oDataTable.ajax.reload();
        });


	//select选择后刷新页面GET请求
    $("#select").change(function(){
		//alert($("#select").val())
		oDataTable.ajax.reload();
	});
	//checkbox全选
    $("#checkAll").on("click", function () {
        if ($(this).prop("checked") === true) {
            $("input[name='checkList']").prop("checked", $(this).prop("checked"));
            $('#example tbody tr').addClass('selected');
        } else {
            $("input[name='checkList']").prop("checked", false);
            $('#example tbody tr').removeClass('selected');
        }
    });


	$("#btnCreate").click(function() {
		var div=layer.open({
			type : 2,
			title : '设备入库',
			shadeClose : false,
			maxmin : true,
			area : [ '800px', '580px' ],
			content : "{% url 'adm-asset:create' %}",
			end : function() {
				//关闭时做的事情
				window.location.reload();
			}
		});
           layer.full(div)
	});


	//批量删除
	$("#btnDelete").click(function() {
		if ($("input[name='checkList']:checked").length == 0){
			layer.msg("请选择要删除的记录");
			return ;
		}

		var arrId= new Array();
		$("input[name='checkList']:checked").each(function(){
			//alert($(this).val());
			arrId.push($(this).val());
		});

		sId=arrId.join(',');

		layer.alert('确定删除吗？', {
			title:'提示'
			,icon: 3 //0:感叹号 1：对号 2：差号 3：问号 4：小锁 5：哭脸 6：笑脸
			,time: 0 //不自动关闭
			,btn: ['YES', 'NO']
			,yes: function(index){
			 	layer.close(index);
			 	$.ajax({
					type: "POST",
					url : "{% url 'adm-asset:delete' %}",
					data : {"id":sId, csrfmiddlewaretoken: '{{ csrf_token }}'},
					cache: false,
					success : function(msg) {
						if (msg.result){
							layer.alert("操作成功",{icon:1});
							oDataTable.ajax.reload();
						}else{
							//alert(msg.message);
							layer.alert("操作失败",{icon:2});
						}
						return ;
					}
				});
			}
		});
	});


});

function doDetail(id){
    window.location.href="{% url 'adm-asset:asset-detail' %}?id="+id;
}

function doUpdate(id){
	var div=layer.open({
		type : 2,
		title : '修改设备信息',
		shadeClose : false,
    	maxmin : true,
		area : [ '800px', '580px' ],
		content : "{% url 'adm-asset:update' %}"+'?id='+id,
		end : function() {
			//oDataTable.ajax.reload();
            window.location.reload();

		}
	});
	layer.full(div)
}

//删除单个数据
function doDelete(id){
    layer.alert('确定删除吗？', {
        title:'提示'
        ,icon: 3 //0:感叹号 1：对号 2：差号 3：问号 4：小锁 5：哭脸 6：笑脸
        ,time: 0 //不自动关闭
        ,btn: ['YES', 'NO']
        ,yes: function(index){
            layer.close(index);
            $.ajax({
                type: "POST",
                url : "{% url 'adm-asset:delete' %}",
                data : {"id":id, csrfmiddlewaretoken: '{{ csrf_token }}'},  //防止post数据时报 csrf_token 403
                cache: false,
                success : function(msg) {
                    if (msg.result){
                        layer.alert('删除成功',{icon: 1});
                        oDataTable.ajax.reload();
                    }else{
                        //alert(msg.message);
                        layer.alert('删除失败', {icon: 2});
                    }
                    return ;
                }
            });
        }
    });

}
</script>
<script>
  $(function () {
    //Initialize Select2 Elements
    $(".select2").select2();
  });
</script>
{% endblock %}